package org.cresource.user.conteoller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.cresource.basic.exception.BusinessException;
import org.cresource.basic.util.AjaxResult;
import org.cresource.basic.util.PageList;
import org.cresource.user.domain.User;
import org.cresource.user.domain.UserDto;
import org.cresource.user.query.UserQuery;
import org.cresource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户 c层
 */
@RequestMapping("/user")
@RestController//返回数据格式为json是@Controller+@ResponseBody
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {


    @Autowired
    private IUserService userService;


    /**
     * 注册  电话注册
     */
     @PostMapping("/register/code")
     public AjaxResult redisterCode(@RequestBody UserDto userDto){
         //把数据给server层  运行成功
         try {
             userService.redisterCode(userDto);
             return  AjaxResult.me();
         }//用户操作错误 
         catch (BusinessException e) {
             return AjaxResult.me().setMessage(e.getMessage());
         }//系统错误
         catch (Exception e) {
             e.printStackTrace();
             return  AjaxResult.me().setMessage("注册c 系统错误");
         }

     }




































    //新增或修改 @PutMapping
    /**
     * @RequestBody 我们从请求体中拿数据
     * @param user
     */
    @PutMapping
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody User user){
            try {
                if(user.getId() == null){
                    userService.add(user);
                }else {
                    userService.update(user);
                }
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me().setMessage("系统繁忙，稍后重试!!!"+e.getMessage());
            }
    }
    //查询一条数据 @GetMapping
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条用户数据",notes = "通过id查询")
    public User getById(@PathVariable("id") Long id){
        return userService.getById(id);
    }
    //删除一条数据 @DeleteMapping
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条用户数据",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试!!!"+e.getMessage());
        }
    }
    //分页查询 @PostMapping
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "通过PageList进行分页")
    public PageList<User> queryPage(@RequestBody UserQuery userQuery){
        return userService.queryPage(userQuery);
    }

    /**
     * 如果前端传入数组，后端用list接收
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
             userService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }
    }

    /**查询所有
     *
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<User> getAll(){
        System.out.println(userService.getAll());
        return userService.getAll();
    }


    
}
